package cn.xx.auth.interceptor;

import cn.xx.auth.constant.CommonConstant;
import cn.xx.auth.entity.User;
import cn.xx.auth.vo.Result;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;

/**
 * 拦截器，配置文件在cn.xx.auth.config.WebConfig，
 * 登录拦截，即验证cookie里的token和redis里的token是不是一个
 */
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("LoginInterceptor preHandle(目标方法执行前执行).....");
        if (handler instanceof HandlerMethod) {
            log.info("方法名："+((HandlerMethod) handler).getMethod().getName());
        }

//        HttpSession session = request.getSession();
//        Enumeration<String> attributeNames = session.getAttributeNames();
//        String s = attributeNames.nextElement();

        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName()+"-------"+ cookie.getValue());
                if (cookie.getName().equals("authToken")) {
                    String value = cookie.getValue();
                    Object o = redisTemplate.opsForValue().get(value);
                    if (o != null) {
                        System.out.println(o);
                        User user = (User) o;
                        System.out.println();
//                        JSONObject jsonObject = JSON.parseObject(o.toString());
//                        System.out.println(jsonObject.get("id"));
//                        System.out.println(jsonObject.get("username"));
//                        System.out.println(jsonObject.get("password"));
                        return true;
                    }
                }
            }
        }


        response.sendRedirect("/login.html");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        log.info("LoginInterceptor postHandle(目标方案执行后执行).....");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        log.info("LoginInterceptor afterCompletion(视图渲染完毕后执行,最后执行).....");
    }
}